// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// compiled file.
//
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require_tree .
//= require bootstrap-sprockets

/**
 *  prepareTransition
 *  jQuery Plugin for ensuring transitions with display:none or visibility:hidden
 *  are in the right state until the end of the transition
 *
 *  Developed by Jonathan Snook (http://snook.ca/)
 *  January 12, 2012
 *
 *  Requires the following CSS:
 *  .is-transitioning {
 *      display: block !important;
 *      visibility: visible !important;
 *  }
 *
 *  MIT license
 *  http://www.opensource.org/licenses/mit-license.php
 */

(function($){

$.fn.prepareTransition = function(){
    return this.each(function(){
        var el = $(this);
        // remove the transition class upon completion
        el.one('TransitionEnd webkitTransitionEnd transitionend oTransitionEnd', function(){
            el.removeClass('is-transitioning');
        });

        // check the various CSS properties to see if a duration has been set
        var cl = ["transition-duration", "-moz-transition-duration", "-webkit-transition-duration", "-o-transition-duration"];
        var duration = 0;
        $.each(cl, function(idx, itm){
            duration || (duration = parseFloat( el.css( itm ) ));
        });

        // if I have a duration then add the class
        if (duration != 0) {
            el.addClass('is-transitioning');
            el[0].offsetWidth; // check offsetWidth to force the style rendering
        };
    });
};


}(jQuery));

(function($) {
  var MyShopAdmin = window.MyShopAdmin = window.MyShopAdmin || {};

  MyShopAdmin.NavDrawer = function() {
    var init, onPrimaryNavItemClick, hideOrExpandSecondaryPannel;

    hideOrExpandSecondaryPannel = function(navDrawerHoveIn) {
      var obj = $('.nav-drawer-pannel-primary .nav-item.current');
      if (obj.length == 1 && obj.hasClass('nav-item-has-submenus')) {
        if (navDrawerHoveIn) {
          $('.nav-drawer-pannel-secondary').prepareTransition();
          $('.nav-drawer').removeClass('secondary-expanded');
        } else {
          $('.nav-drawer-pannel-secondary').prepareTransition();
          $('.nav-drawer').addClass('secondary-expanded');
        }
      }
    };

    onPrimaryNavItemClick = function(event) {
      event.preventDefault();

      var obj = $(this);
      if (obj.hasClass('nav-item-has-submenus')) {
        var submenus = obj.attr('data-sub-menus');
        $('.nav-drawer').addClass('secondary-expanded');
        $('.nav-drawer-pannel-secondary .nav-sub-menus').removeClass('active');
        $('.nav-drawer-pannel-secondary .nav-sub-menus-' + submenus).addClass('active');
        $('.nav-drawer-pannel-primary .nav-item').removeClass('current');
        obj.addClass('current');
      }
    };

    init = function() {
      $('.nav-drawer-pannel-primary .nav-item').click(onPrimaryNavItemClick);
      $('.nav-drawer-pannel-primary').hover(function() {
        hideOrExpandSecondaryPannel(true);
      }, function() {
        hideOrExpandSecondaryPannel(false);
      });
    };

    return {
      init: init
    };
  }();

  $(function() {
    MyShopAdmin.NavDrawer.init();
  });
})(jQuery);